import pygame
from pygame.locals import *

import gameobject
import locals

def main():
    # Init Pygame and setup a screen surface
    pygame.init()
    screen = pygame.display.set_mode((locals.SCREEN['WIDTH'], locals.SCREEN['HEIGHT']))
    
    # Create three containers for DirtySprites and GameObjects
    objectManager = pygame.sprite.LayeredDirty()    # Container for GameObjects
    bgManager = pygame.sprite.LayeredDirty()        # Container for the background
    fgManager = pygame.sprite.LayeredDirty()        # Container for the foreground
    
    # Here we setup a simple GameObject
    testSpr = gameobject.GameObject("spr.png") 
    testSpr.setup(jumping = False, falling = False, visible = 1, dirty = 2)
    testSpr.setCenterX(locals.SCREEN['WIDTH']/2)
    testSpr.setCenterY((locals.SCREEN['HEIGHT']/2)+100-testSpr.rect.height)
    
    testSpr2 = gameobject.GameObject("ground.png") 
    testSpr2.setup(visible = 1, dirty = 2)
    testSpr2.rect.centery = (locals.SCREEN['HEIGHT']/2)+100
    testSpr2.rect.centerx = locals.SCREEN['WIDTH']/2
    
    testSpr22 = gameobject.GameObject("ground.png") 
    testSpr22.setup(visible = 1, dirty = 2)
    testSpr22.rect.centery = (locals.SCREEN['HEIGHT']/2) - 50
    testSpr22.rect.centerx = locals.SCREEN['WIDTH']/2 + 200
    
    testSpr3 = gameobject.GameObject("bgtest1.png") 
    testSpr3.setup(visible = 1, dirty = 2)
    
    objectManager.add(testSpr)
    fgManager.add(testSpr2)
    fgManager.add(testSpr22)
    bgManager.add(testSpr3)
    
    testSpr.addCollisionGroup(fgManager)
    
    # Setup a background, as a Surface for now
    background = pygame.Surface((locals.SCREEN['WIDTH'], locals.SCREEN['HEIGHT']))
    background.fill((23, 45, 67))
    
    # Setup FPS limiter
    clock = pygame.time.Clock()
    fps = 1*60
    
    # Initial render
    pygame.display.update()
                         
    while True:
        # Limit the speed
        clock.tick(fps)
                                                
        # Handle input
        for ev in pygame.event.get():
            if ev.type == QUIT:
                exit(0)
            if ev.type == KEYDOWN:
                if ev.key == K_ESCAPE:
                    exit(0)
                
        #testSpr.move(direction, 0)
        
        # Handle logic                  
        # Check for collisions
        testSpr.update()
            
        if pygame.sprite.spritecollideany(testSpr, fgManager):
            print "touch"
                
        # Handle graphics
        bgManager.draw(screen, background)
        objectManager.draw(screen, bgManager.get_sprite(0).image)
        fgManager.draw(screen, background)
        pygame.display.update()